//
//  CustomerCell.m
//  KuWoCan
//
//  Created by LiBenCan on 17/9/26.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "CustomerCell.h"
#import "UILabel+HWDUILabel.h"
@implementation CustomerCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        self.backgroundColor = [UIColor colorWithHexString:@"#EFF0F5"];
    }
    return self;
}

- (void)loadCell
{
    if (!_baseView) {//底层View
        _baseView = [UIView new];
        _baseView.backgroundColor = [UIColor whiteColor];
        [_baseView.layer setCornerRadius:5*screenW_6];
        _baseView.layer.masksToBounds = YES;
        [self.contentView addSubview:_baseView];
        [_baseView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView).offset(10);
            make.top.equalTo(self.contentView).offset(10);
            make.right.equalTo(self.contentView).offset((-10));
            make.bottom.equalTo(self.contentView).offset(0);
        }];
    }
    //移除子视图
    for (UIView *subView in _baseView.subviews) {
        [subView removeFromSuperview];
    }
    _shopNameLab = [UILabel new];
    _shopNameLab.textColor = [UIColor colorWithHexString:@"#333333"];
    _shopNameLab.font = [UIFont systemFontOfSize:16*screenW_6];
    _shopNameLab.textAlignment = NSTextAlignmentLeft;
    _shopNameLab.text = @"中百经理";
    _shopNameLab.numberOfLines = 0;
    [_baseView addSubview:_shopNameLab];
    [_shopNameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(15);
        make.top.equalTo(self.baseView).offset(20);
        make.right.equalTo(_baseView).offset((-120*screenW_6));
    }];
    
    _editLab = [UILabel new];
    _editLab.textColor = [UIColor colorWithHexString:@"#ff8726"];
    _editLab.attributedText =[self setStringWithUIImage:@"cus_edit" Content:@"  编辑"];
    _editLab.hidden = YES;
    _editLab.userInteractionEnabled = YES;
    [_baseView addSubview:_editLab];
    [_editLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.shopNameLab.mas_right).offset(5);
        make.top.equalTo(_shopNameLab).offset(0);
        make.height.equalTo(@20);
        make.width.equalTo(@(50*screenW_6));
    }];
    
    _deleteLab = [UILabel new];
    _deleteLab.textColor = [UIColor colorWithHexString:@"#ff8726"];
    _deleteLab.attributedText =[self setStringWithUIImage:@"cus_delet" Content:@"  删除"];
    _deleteLab.userInteractionEnabled = YES;
    _deleteLab.hidden = YES;
    [_baseView addSubview:_deleteLab];
    [_deleteLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_editLab.mas_right).offset(5);
        make.top.equalTo(_shopNameLab).offset(0);
        make.height.equalTo(@20);
        make.right.equalTo(_baseView).offset((-10*screenW_6));;
    }];
    
    _timeLab = [UILabel new];
    _timeLab.textColor = [UIColor grayColor];
    _timeLab.font = [UIFont systemFontOfSize:11*screenW_6];
    _timeLab.textAlignment = NSTextAlignmentLeft;
    _timeLab.text = @"2017.09.25 13:55";
    [_baseView addSubview:_timeLab];
    [_timeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(15);
        make.top.equalTo(_shopNameLab.mas_bottom).offset(5);
        make.height.equalTo(@(11*screenW_6));
        make.right.equalTo(_baseView).offset((-10));
    }];
    
    _nameLab = [UILabel new];
    [_baseView addSubview:_nameLab];
    _nameLab.textAlignment = NSTextAlignmentLeft;
    _nameLab.textColor = [UIColor colorWithHexString:@"#333333"];
    [_nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.baseView).offset(15);
        make.top.equalTo(_timeLab.mas_bottom).offset(15);
        make.height.equalTo(@(20*screenW_6));
    }];
    
    _phoneLab = [UILabel new];
    [_baseView addSubview:_phoneLab];
    _phoneLab.textAlignment = NSTextAlignmentLeft;
    _phoneLab.textColor = [UIColor colorWithHexString:@"#333333"];
    [_phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_nameLab.mas_right).offset(10);
        make.top.equalTo(_timeLab.mas_bottom).offset(15);
        make.height.equalTo(@(20*screenW_6));
    }];
    
    _bgphoneLab = [UILabel new];
    [_baseView addSubview:_bgphoneLab];
    _bgphoneLab.textAlignment = NSTextAlignmentLeft;
    _bgphoneLab.textColor = [UIColor colorWithHexString:@"#333333"];
    [_bgphoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_phoneLab.mas_right).offset(10);
        make.top.equalTo(_timeLab.mas_bottom).offset(15);
        make.height.equalTo(@(20*screenW_6));
        make.right.equalTo(_baseView).offset((-10));
    }];
    
    _addressImageV = [UIImageView new];
    _addressImageV.image = [UIImage imageNamed:@"cus_add"];
    [_baseView addSubview:_addressImageV];
    [_addressImageV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_baseView).offset(15);
        make.top.equalTo(_phoneLab.mas_bottom).offset(12);
        make.width.equalTo(@(11*screenW_6));
        make.height.equalTo(@(12*screenW_6));
    }];
    
    _addressLab = [UILabel new];
    [_baseView addSubview:_addressLab];
    _addressLab.textAlignment = NSTextAlignmentLeft;
    _addressLab.font = [UIFont systemFontOfSize:13*screenW_6];
    _addressLab.textColor = [UIColor colorWithHexString:@"#333333"];
    _addressLab.numberOfLines = 0;
    [_addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_addressImageV.mas_right).offset(0);
        make.top.equalTo(_phoneLab.mas_bottom).offset(5);
        make.right.equalTo(_baseView).offset((-15));
        make.bottom.equalTo(_baseView).offset((-10));
    }];

}

-(void)setDataDic:(NSDictionary *)dataDic//添加数据
{
    //录入时间
    _timeLab.text = [NSString stringWithFormat:@"%@",[ToolViewViewController GetDataStr:dataDic[@"createDate"]]];
    //客户名称
    _shopNameLab.text = [NSString stringWithFormat:@"%@",[ToolViewViewController GetDataStr:dataDic[@"name"]]];
    //联系人
    _nameLab.attributedText = [self setStringWithUIImage:@"cus_name" Content:[NSString stringWithFormat:@" %@",[ToolViewViewController GetDataStr:dataDic[@"clientContacts"]]]];
    float nameWidth =  [ToolViewViewController getWidthFromString:[NSString stringWithFormat:@" %@",[ToolViewViewController GetDataStr:dataDic[@"clientContacts"]]] Font:13*screenW_6];
    //联系电话
    _phoneLab.attributedText = [self setStringWithUIImage:@"cus_phone" Content:[NSString stringWithFormat:@" %@",[ToolViewViewController GetDataStr:dataDic[@"clientPhone"]]]];
     float phoneWidth =  [ToolViewViewController getWidthFromString:[NSString stringWithFormat:@" %@",[ToolViewViewController GetDataStr:dataDic[@"clientPhone"]]] Font:13*screenW_6];
    //办公电话
    _bgphoneLab.attributedText = [self setStringWithUIImage:@"cus_bgphone" Content:[NSString stringWithFormat:@" %@",[ToolViewViewController GetDataStr:dataDic[@"clientTel"]]]];
    //地址
    _addressLab.text = [NSString stringWithFormat:@" %@",[ToolViewViewController GetDataStr:dataDic[@"allAddress"]]];
    [_addressLab setLineSpaceWithLineSpacing:5 text:_addressLab.text];

    [_nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(nameWidth + 20));
    }];
    [_phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(phoneWidth + 20));
    }];
    _addressLab.frame = CGRectMake(_addressLab.frame.origin.x,_addressLab.frame.origin.y, SCREENW-66, 0);
    [_addressLab sizeToFit];
    [_addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(_addressLab.frame.size.height + 10));
    }];
    
    if (![ToolViewViewController isNotNullStr:dataDic[@"clientTel"]]) {
        _bgphoneLab.hidden = YES;
    }else
    {
        _bgphoneLab.hidden = NO;
    }
    if (![ToolViewViewController isNotNullStr:dataDic[@"allAddress"]]) {
        _addressImageV.hidden = YES;
        [_addressImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@5);
        }];
        [_addressLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@5);
        }];
    }else
    {
        _addressImageV.hidden = NO;
        [_addressImageV mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(12*screenW_6));
        }];
    }
}
-(void)setIsNowEdit:(BOOL)isNowEdit//编辑删除按钮显隐
{
    _editLab.hidden = _deleteLab.hidden = isNowEdit;
}
 // 创建一个富文本
- (NSAttributedString *)setStringWithUIImage:(NSString *)imageName Content:(NSString *)contentStr {
    
    NSMutableAttributedString * attriStr = [[NSMutableAttributedString alloc] initWithString:contentStr];
    // 修改富文本中的不同文字的样式
    [attriStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:13*screenW_6] range:NSMakeRange(0,contentStr.length)];
    
    //添加图片到指定的位置
    NSTextAttachment *attchImage = [[NSTextAttachment alloc] init];
    // 表情图片
    attchImage.image = [UIImage imageNamed:imageName];
    // 设置图片大小
    
    attchImage.bounds = CGRectMake(0,-2,attchImage.image.size.width,attchImage.image.size.height);
    NSAttributedString *stringImage = [NSAttributedString attributedStringWithAttachment:attchImage];
    [attriStr insertAttributedString:stringImage atIndex:0];
    
    return attriStr;
}


@end
